package com.kasax.plugin.shadow

import com.android.build.gradle.AppExtension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension
import com.kasax.plugin.shadow.utils.LogUtils
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project

class AppMainPlugin implements Plugin<Project> {
    private static final String LOG_TAG = "AppMainPlugin"

    void apply(Project project) {
        LogUtils.d(LOG_TAG, "apply")

        LogUtils.init(project)
        // 编译期仅依赖 AGP，运行期由宿主提供实现
//        project.dependencies.add('compileOnly', 'com.android.tools.build:gradle:4.2.2')
//        project.dependencies.add('compileOnly', 'org.ow2.asm:asm:9.5')
//        project.dependencies.add('compileOnly', 'org.ow2.asm:asm-commons:9.5')

        def extension = project.extensions.create('renamePackage', RenamePackageExtension)
//
//        project.afterEvaluate {
//            def appExttesn = project.extensions.getByType(BaseExtension)
//            // 移除对 Library 模块的限制
//
//            appExttesn.libraryVariants.all { variant ->
//                def transform = new PackageRenameTransform(project, extension, variant)
//                LogUtils.d(LOG_TAG, "registerTransform for variant: ${variant.name}")
//                appExttesn.registerTransform(transform)
//                LogUtils.d(LOG_TAG, "Transform registered for variant: ${variant.name}")
//            }
//        }

        if (project.plugins.hasPlugin(AppExtension)) {
            LogUtils.d(LOG_TAG, "registerTransform")
            def appExt = project.extensions.getByType(AppExtension)
            appExt.registerTransform(new PackageRenameTransform())
            LogUtils.d(LOG_TAG, "registerTransform complete")

        }
    }
}